/*
 * Copyright (c) 2020 solidwang. All rights reserved.
 */

package com.solidwang.mybatis.controller;

import com.solidwang.mybatis.model.User;
import com.solidwang.mybatis.param.UserParam;
import com.solidwang.mybatis.response.Code;
import com.solidwang.mybatis.response.ResponseUtil;
import com.solidwang.mybatis.response.SimpleResult;
import com.solidwang.mybatis.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "保存用户接口", httpMethod = "POST")
    @PostMapping(value = "/save", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public SimpleResult<Object> save(@Valid UserParam userParam, BindingResult results) {
        if (results.hasErrors()) {
            return ResponseUtil.writeResult(Code.valueOf(Code.PARAM_ERROR.getCode()), results.getFieldError().getDefaultMessage());
        }
        String name = userParam.getName();
        Integer age = userParam.getAge();
        Timestamp ctime = new Timestamp(System.currentTimeMillis());
        Timestamp utime = new Timestamp(System.currentTimeMillis());
        Integer num = userService.insert(name, age, ctime, utime);
        if (num == 1) {
            return ResponseUtil.writeResult(Code.valueOf(Code.SUCCESS.getCode()), num);
        }
        return ResponseUtil.writeResult(Code.valueOf(Code.SERVER_ERROR.getCode()));
    }

    @ApiOperation(value = "删除用户接口", httpMethod = "POST")
    @PostMapping(value = "/delete", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public SimpleResult<Object> delete(Integer id) {
        userService.deleteById(id);
        return ResponseUtil.writeResult(Code.valueOf(Code.SUCCESS.getCode()));
    }

    @ApiOperation(value = "修改用户接口", httpMethod = "POST")
    @PostMapping(value = "/update", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public SimpleResult<Object> update(Integer id, String name) {
        userService.updataById(id, name);
        return ResponseUtil.writeResult(Code.valueOf(Code.SUCCESS.getCode()));
    }

    @ApiOperation(value = "查找用户接口", httpMethod = "GET")
    @GetMapping(value = "/findById", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public SimpleResult<Object> findById(Integer id) {
        User user = userService.findById(id);
        return ResponseUtil.writeResult(Code.valueOf(Code.SUCCESS.getCode()), user);
    }

    @ApiOperation(value = "查找所有用户接口", httpMethod = "GET")
    @GetMapping(value = "/findAllUser", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public SimpleResult<Object> findAllUser() {
        List<User> users = userService.findAllUser();
        return ResponseUtil.writeResult(Code.valueOf(Code.SUCCESS.getCode()), users);
    }
}